package com.sc.web.quartz;


import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.sc.dal.dto.order.TSaleDeliverResult;
import com.sc.dal.dto.order.TSaleDelivery;
import com.sc.dal.dto.order.TSaleOrder;
import com.sc.dal.dto.order.TSaleOrderResult;
import com.sc.dal.model.member.MemberNews;
import com.sc.dal.model.order.Order;
import com.sc.dal.model.order.Orders;
import com.sc.service.order.OrderService;
import com.sc.util.data.Common;
import com.sc.util.data.Constants;
import com.sc.util.data.DateUtil;
import com.sc.util.openAPI.TOpenAPIUtil;
/**
 * @ClassName: SaleDeliveryTask
 * @Description:向T+系统拉去销货单执行状态（暂时业务用不到)
 * @author LeiJia
 * @date 2017年4月15日 下午2:39:23
 * @version V1.0
 */

public class  SaleOrderJob{	
	
	private final static Logger log = LoggerFactory.getLogger(SaleOrderJob.class);
	
	@Autowired
	private OrderService orderService;
	
	public void saleOrderTask() {
		long start = System.currentTimeMillis();
		log.info(Common.printTimeStamp() + "向T+系统拉取销售订单执行状态任务开始了。。。。，开始中。。。");
		
		try {
			
			//读取全部未发货的订单列表
			Orders order = new Orders();
			order.setQueryStatus("2");
			List<Orders> orderList = orderService.selectByParam(order);
			for(Orders o : orderList){
			    String orderId = o.getOrderId();
			    //向T+系统查询该订单的发货状态
			    TSaleOrderResult result = TOpenAPIUtil.getTSaleOrderByExternalCode(orderId,false);
			    if(result != null){
			    	 List<TSaleOrder> tsaleOrderList =  result.getRows();
			    	 if(tsaleOrderList != null && tsaleOrderList.size() >0){
			    		 TSaleOrder saleOrder = tsaleOrderList.get(0);
			    		 String deliverDate = saleOrder.getDeliveryDate();
			    		 String deliverCode = saleOrder.getSaleDeliveryCode();
			    		 if(Common.isEmpty(deliverDate)){
			    			 if(Common.isNotEmpty(deliverCode)){
			    				 //查询销货单执行情况
			    				 TSaleDeliverResult deliverResult =  TOpenAPIUtil.getTSaleDeliverByDeliverCode(deliverCode,false);
			    				 List<TSaleDelivery> saleDeliveryList = deliverResult.getRows();
			    				 if(saleDeliveryList !=null){
				    				 TSaleDelivery delivery = saleDeliveryList.get(0);
				    				 deliverDate = delivery.getDeliveryDate();
			    				 }
			    			 }
			    		 }
			    		 if(Common.isNotEmpty(deliverDate)){
				    		 updateOrderShipStatus(order,deliverDate);			    			 
			    		 }
			    	 }
			    }
			}
		} catch (Exception e) {    
			e.printStackTrace();
		}		
		
		long end = System.currentTimeMillis();
		long hj = end - start;
		log.info(Common.printTimeStamp() + "向T+系统拉取销售订单单执行状态任务结束了。。。" + "共计用了：" + hj / 1000 + "秒");
		
	}
	
	/**
	 * @Title: updateOrderShipStatus
	 * @Description 记录订单发货状态
	 * @param orders
	 * @param deliverDate
	 * @date 2017年4月17日上午16:08:35
	 * @author LeiJia  
	 * @return 
	 */
	public void updateOrderShipStatus(Orders orders, String deliverDate){
         String orderId = orders.getOrderId();
         String memerberId = orders.getMemberId();
		 Order order_ =new Order();
		 order_.setOrderId(orderId);
		 order_.setShipTime(DateUtil.formatDateYMDHMSStrToDate(deliverDate));
		 order_.setShipStatus(Constants.OrderDetail.OrderDeliver.SHIP_STATUSB);
		 //修改订单的发货状态
		 int shipStatus =  orderService.updateOrder(order_);
		 if(shipStatus > 0){
			 //生成一条交易物流消息通知
			 MemberNews news = new MemberNews(); //交易物流消息
			 news.setCreater("T+");
	    	 news.setModifier("T+");
			 news.setOrderId(orderId);
	    	 news.setMemberId(memerberId);
			 news.setNewsTitle(Constants.OrderDetail.OrderDeliver.SHIP_STATUS_TITLEB);		    	
             orderService.insertMemberNews(news);
		 }
	}
}
